﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CaiBaby.Common;
using CaiBaby.Entity;

namespace CaiBaby.DAL
{
    /// <summary>
    /// 第三方帐号表数据访问层
    /// </summary>
    public class Oauth
    {
        /// <summary>
        /// 创建一个新第三方帐号
        /// </summary>
        /// <param name="oauthInfo">第三方帐号实体</param>
        /// <returns>返回第三方帐号ID</returns>
        public int Add(OauthInfo oauthInfo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO us_Oauth(");
            strSql.Append("UId,Thetype,Thekey,CreateTime,LastLogTime,LastLogIp)");
            strSql.Append(" VALUES (");
            strSql.Append("@UId,@Thetype,@Thekey,@CreateTime,@LastLogTime,@LastLogIp)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@UId", SqlDbType.Int,4),
					new SqlParameter("@Thetype", SqlDbType.Int,4),
					new SqlParameter("@Thekey", SqlDbType.VarChar,128),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@LastLogTime", SqlDbType.DateTime),
					new SqlParameter("@LastLogIp", SqlDbType.VarChar,20)};
            parameters[0].Value = oauthInfo.UId;
            parameters[1].Value = oauthInfo.Thetype;
            parameters[2].Value = oauthInfo.Thekey;
            parameters[3].Value = oauthInfo.CreateTime;
            parameters[4].Value = oauthInfo.LastLogTime;
            parameters[5].Value = oauthInfo.LastLogIp;

            object obj = SQLHelper.GetSingle(ConfigHelper.CaiBabyDBConnStr,CommandType.Text,strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
    }
}
